package com.kingyea.app.market.utils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数工具，主要是处理获取的参数：eg.从map里获取String
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2017/11/23 20:23
 */
public class StringUtils {

    /**
     * Object 转 String
     *
     * @param obj
     * @return java.lang.String
     * @author Mr.Lin
     * @date 2017/11/24 10:47
     */
    public static String obj2String(Object obj) {
        if (org.springframework.util.StringUtils.isEmpty(obj)) {
            return null;
        } else {
            return obj.toString();
        }
    }

    /**
     * 所有key,首字母小写
     *
     * @param map
     * @return java.util.Map<java.lang.String                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                               java.lang.Object>
     * @author Mr.Lin
     * @date 2017/12/5 20:19
     */
    public static Map<String, Object> toLowerCaseFirstOne4MapKey(Map<String, Object> map) {
        Map<String, Object> temMap = new HashMap<>();
        map.forEach((k, v) -> {
            char[] cs = k.toCharArray();
            if (Character.isUpperCase(cs[0])) {
                cs[0] += 32;
            }
            k = String.valueOf(cs);
            if (v instanceof List) {
                List<Map<String, Object>> tempList = new ArrayList<>();
                ((List<Map<String, Object>>) v).forEach(item -> tempList.add(toLowerCaseFirstOne4MapKey(item)));
                v = tempList;
            }
            temMap.put(k, v);
        });
        return temMap;
    }

    public static String getUrlFileName(String url) {
        return null == url ? null : url.substring(url.lastIndexOf("/") + 1);
    }

}